package com.thering;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * author: the_ring
 * date: 2022/5/27
 * function: Type Count main
 */
public class TypeCount {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        // 获取昨天
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date date = cal.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(date);

        // 配置环境
//        Properties properties = System.getProperties();
//        properties.setProperty("HADOOP_USER_NAME", "root");
        Configuration conf = new Configuration();
        conf.set("date", yesterday);
//        conf.set("fs.defaultFS", "hdfs://ring1:8020");

        // job 配置
        Job job = Job.getInstance(conf);
        job.setJobName("type count");
        job.setJarByClass(TypeCount.class);

        // map 配置
        job.setMapperClass(TypeCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        TableMapReduceUtil.initTableReducerJob(
                "movie_type_count",
                TypeCountReducer.class,
                job
        );

        FileInputFormat.addInputPath(job, new Path("/project/" + yesterday + "/movies/part-m*"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }


}
